/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.bean.custom;

import com.jb.framework.PersistentContext;
import com.jb.web.bean.FloatPanel;
import com.jb.web.context.WebContext;


public class Splitter extends FloatPanel {
    static final long serialVersionUID = 123123L;
    private String direction = "horizontal";
    private FloatPanel p1;
    private FloatPanel p2;
    private FloatPanel p3;

    static {
        publish(Splitter.class, GENERAL, "direction");
    }

    public void updateView(WebContext pCtx) {
        p1 = (FloatPanel)getChildByClientId(this.getClientId() + "1");
        p2 = (FloatPanel)getChildByClientId(this.getClientId() + "s");
        p3 = (FloatPanel)getChildByClientId(this.getClientId() + "2");

        if (p1 != null && p2!=null && p3!=null) {
            {
                this.setOnLoad("JB.splitterLoaded('" + p2.getClientId() + 
                               "', '" + p1.getClientId() + "', '" + 
                               p3.getClientId() + "', '" + this.getClientId() + 
                               "', '" + direction + "');");
            }
            super.updateView(pCtx);
        }
    }

    public void setDefault(PersistentContext pCtx) {
        setStyleClass(null);
        this.setHeight("400px");
        this.setWidth("606px");
        this.setBackgroundStyle("background-color:silver;");
        p1 = new FloatPanel();
        p1.setClientId(this.getClientId() + "1");
        p1.setStyleClass(null);
        p1.setWidth("300px");
        p1.setDeletable(false);
        this.addChild(p1);
        p1.setBackgroundStyle("background-color:green;");
        p1.setOverFlow("auto");

        p2 = new FloatPanel();
        p2.setClientId(this.getClientId() + "s");
        p2.setStyleClass(null);
        p2.setDraggable(true);
        p2.setCloneOnDrag(true);
        p2.setDeletable(false);
        p2.setWidth("3px");
        p2.setBackgroundStyle("background-color:gray;");
        p2.setDragWithin(getClientId());

        this.addChild(p2);
        p3 = new FloatPanel();
        p3.setBackgroundStyle("background-color:red;");
        p3.setClientId(this.getClientId() + "2");
        p3.setStyleClass(null);
        p3.setWidth("300px");
        p3.setDeletable(false);
        p3.setOverFlow("auto");
        this.addChild(p3);
        p1.setFloatStyle("left");
        p2.setFloatStyle("left");
        p3.setFloatStyle("left");
        directionChanged();
    }

    public void setDirection(String direction) {
        if (!this.direction.equals(direction)) {
            this.direction = direction;
            directionChanged();
        }
    }

    private void directionChanged() {
        p1 = (FloatPanel)getChildByClientId(this.getClientId() + "1");
        p2 = (FloatPanel)getChildByClientId(this.getClientId() + "s");
        p3 = (FloatPanel)getChildByClientId(this.getClientId() + "2");

        if (p1 != null) {
            if ("horizontal".equals(direction)) {
                p1.setFloatStyle("left");
                p2.setFloatStyle("left");
                p3.setFloatStyle("left");

                p2.setWidth("3px");
                p1.setHeight("100%");
                p2.setHeight("100%");
                p3.setHeight("100%");

                p1.setWidth("300px");
                p3.setWidth("300px");
                setHeight("400px");
                setWidth("606px");
            } else {
                p1.setFloatStyle("");
                p2.setFloatStyle("");
                p3.setFloatStyle("");

                p2.setHeight("3px");
                p1.setWidth("100%");
                p2.setWidth("100%");
                p3.setWidth("100%");

                p1.setHeight("300px");
                p2.setHeight("3px");
                p3.setHeight("300px");
                this.setHeight("604px");
            }
            p2.setOnMouseUp("JB.splitterMoved('" + p2.getClientId() + "', '" + 
                            p1.getClientId() + "', '" + p3.getClientId() + 
                            "', '" + this.getClientId() + "', '" + direction + 
                            "');");
        }
    }

    public String getDirection() {
        return direction;
    }
}
